package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.moving.R;
import com.lanou.moving.activity.SingerDetailActivity;
import com.lanou.moving.adapter.SearchSingerAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.entity.SearchSingerRoot;
import com.lanou.moving.utils.MyConfig;
import com.lanou.moving.utils.NoScrollListView;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.VolleySingleton;
import java.util.ArrayList;

/**
 * Created by dllo on 15/10/23.
 */
public class SingleFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private ArrayList<SearchSingerRoot.DataEntity> datas;
    private String url;
    private VolleySingleton volleySingleton;
    private SearchSingerAdapter adapter;
    private NoScrollListView noScrollListView;
    private GsonRequest<SearchSingerRoot> rootGsonRequest;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_singer_page, null);
        noScrollListView = (NoScrollListView) view.findViewById(R.id.singer_list);
        noScrollListView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance(mContext);
        initData();
        return view;
    }

    private void initData() {
        //接收数据替换q
        final String q = MyConfig.getSharePreStr(mContext, "Searchcontent", "url");
        Log.i("da", "++" + q);
        url = "http://so.ard.iyyin.com/s/artist?uid=866058021842951&f=f3040&app=ttpod&hid=1135212123029932&alf=alf203200&net=2&" +
                "size=50&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&page=1&q=" +
                q +
                "&imsi=460078098497828&tid=0";
        rootGsonRequest = new GsonRequest<SearchSingerRoot>(url, SearchSingerRoot.class, new Response.Listener<SearchSingerRoot>() {
            @Override
            public void onResponse(SearchSingerRoot response) {
                datas = (ArrayList<SearchSingerRoot.DataEntity>) response.getData();
                adapter = new SearchSingerAdapter(getActivity(), datas);
                noScrollListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(rootGsonRequest);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchSingerRoot.DataEntity entity = (SearchSingerRoot.DataEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(getActivity(), SingerDetailActivity.class);
        intent.putExtra("singer_id", entity.get_id());
        startActivity(intent);
        getActivity().finish();

    }


}
